// SmallRocketSpawner.ts
import { _decorator, Component, Node, Prefab, instantiate, Vec3 } from 'cc';
import { SmallRocket } from './weapon/SmallRocket'; // 确保正确导入 SmallRocket 类
const { ccclass, property } = _decorator;

@ccclass('SmallRocketSpawner')
export class SmallRocketSpawner extends Component {
    @property({ type: Prefab })
    smallRocketPrefab: Prefab = null; // 小火箭弩的预制体

    // 方法用于生成并发射 SmallRocket
    spawnAndShootSmallRocket(position: Vec3, index: number) {
        if (!this.smallRocketPrefab) {
            console.error('SmallRocket prefab is not assigned');
            return;
        }

        // 实例化 SmallRocket 预制体
        const smallRocket = instantiate(this.smallRocketPrefab);
        smallRocket.setParent(this.node); // 将生成的实例添加到场景中
        smallRocket.setPosition(position);

        // 计算发射方向
        let angle = index * 120; // 三个火箭弩分别相差120度
        let direction = new Vec3(Math.cos(angle), Math.sin(angle), 0);

        // 设置 SmallRocket 的属性和发射
        let smallRocketScript = smallRocket.getComponent(SmallRocket);
        if (smallRocketScript) {
            smallRocketScript.attackPower = 50 / 2; // 假设攻击力是50的一半
            // smallRocketScript.shoot(direction); // 发射 SmallRocket
        } else {
            console.error("SmallRocket component not found on the prefab");
        }
    }
}
